package com.hiking.jwt.service;


import com.hiking.jwt.domain.UserBean;
import com.hiking.jwt.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    public UserBean register(UserBean userBean) {
        if (userRepository.findByUsername(userBean.getUsername()) == null) {
            userBean.setToken("t" + userBean.getUsername());
            return userRepository.save(userBean);
        }
        return null;
    }

    public UserBean login(UserBean userBean) {
        return userRepository.findByUsernameAndPassword(userBean.getUsername(), userBean.getPassword());
    }

    public ArrayList<UserBean> findAll() {
        return (ArrayList<UserBean>) userRepository.findAll();
    }

}
